package com.huilai.platform.country_platform.controller;

import com.huilai.platform.country_platform.core.Msg;
import com.huilai.platform.country_platform.core.Result;
import com.huilai.platform.country_platform.dto.*;
import com.huilai.platform.country_platform.model.*;
import com.huilai.platform.country_platform.service.*;
import com.huilai.platform.country_platform.util.CookieUtil;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：洪霓
 * 日期：2022/11/25
 * 注释：
 */
@Controller
public class IndexController {
    @Autowired
    private AttractionsService attractionsService;

    @Autowired
    private PartyService partyService;

    @Autowired
    private VillageService villageService;

    @Autowired
    private ChatService chatService;

    @Autowired
    private UserService userService;

    @Autowired
    private InformationService informationService;

    @RequestMapping("/auditResult")
    public String auditResult(Model model, HttpServletRequest request, HttpSession httpSession){
        Cookie cookie = CookieUtil.get(request, "username");
        User user = userService.getUserByUsername(cookie.getValue());
        if (cookie == null) {
            throw new ServiceException("用户未登录");
        }
        Integer userId = user.getId();
        Pageable pageable = PageRequest.of(0, 5);
        Page<FeedbackResultDTO> page = villageService.toVillageFeedbackListUI(pageable);
        List<String> typeList = new ArrayList<>();
        typeList.add("检举控告");
        typeList.add("群众诉求");
        typeList.add("批评建议");
        model.addAttribute("page", page);
        model.addAttribute("typeList", typeList);
        return "information/auditResult";
    }
    @RequestMapping("/auditResult2")
    public String auditResult2(Model model, HttpServletRequest request, @PageableDefault(size = 10)Pageable pageable){
        Page<RecruitmentDTO> page = informationService.auditPushRecruitmentListUI(request, pageable);
        model.addAttribute("page",page);
        return "information/auditResult2";
    }

    @RequestMapping("/chat")
    public String chat(Model model, HttpSession httpSession){
        //获取httpSession中的user值
        User user = (User) httpSession.getAttribute("user");
        Integer fromId = userService.getUserByUsername(user.getUsername()).getId();
        List<ChatListDTO> chatListDTOList = chatService.getFromUserChatList(fromId);
        model.addAttribute("chatListDTOList",chatListDTOList);
        model.addAttribute("fromName", user.getUsername());
        return "information/chat";
    }

    @GetMapping("/intoChat")
    public String recentChatRecords(@RequestParam("linkId")Integer linkId, @RequestParam("toName")String toName,
                                    @RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                    Model model,
                                    HttpSession httpSession){

        User user = (User) httpSession.getAttribute("user");
        Integer fromId = userService.getUserByUsername(user.getUsername()).getId();
        System.out.println(fromId);
        List<ChatMessageDTO> chatMessageDTOList = chatService.getRecentChatRecords(linkId, fromId, userService.getUserByUsername(toName).getId(),pageNum, pageSize);
        List<ChatListDTO> chatListDTOList = chatService.getFromUserChatList(fromId);
        model.addAttribute("linkId", linkId);
        model.addAttribute("toName", toName);
        model.addAttribute("toId",userService.getUserByUsername(toName).getId());
        model.addAttribute("fromId", fromId);
        model.addAttribute("fromName", user.getUsername());
        model.addAttribute("chatListDTOList", chatListDTOList);
        model.addAttribute("chatMessageDTOList", chatMessageDTOList);

        //获取最近的聊天记录
        return "information/intoChat";

    }


    @RequestMapping("/layout")
    public String test2(Model model){
        return "base/layout";
    }

    @RequestMapping("/")
    public String index(Model model, @ModelAttribute("contentType")String contentType){
//        智慧党建、乡务公开
        List<PartyActivities> partyActivitiesList1 = partyService.getPartyActivitiesList("党员活动");
        List<PartyActivities> partyActivitiesList2 = partyService.getPartyActivitiesList("党建动态");
        List<PartyActivities> partyActivitiesList3 = partyService.getPartyActivitiesList("三会一课");

        List<VillageActivities> vllageActivities1 = villageService.getVillageActivitiesList("政务公开");
        List<VillageActivities> vllageActivities2 = villageService.getVillageActivitiesList("政策法规");
        List<VillageActivities> vllageActivities3 = villageService.getVillageActivitiesList("办事指南");
        List<VillageCommittee> villageCommitteeList = villageService.getvillageCommitteeList();
        List<VillageCommittee> villageCommitteeList2 = villageService.getvillageCommitteeList2();
        VillageCommittee zhenzhang = villageService.getZhenzhang();
        VillageCommittee shuji = villageService.getShuji();
//        景点、图片轮播
        List<AttractionsWaterscape> waterscapeList = attractionsService.getTopWaterscape();
        List<AttractionsBuilding> buildingList = attractionsService.getTopBuilding();
        List<AttractionsFood> foodList = attractionsService.getTopFood();
        model.addAttribute("partyActivitiesList1", partyActivitiesList1);
        model.addAttribute("partyActivitiesList2", partyActivitiesList2);
        model.addAttribute("partyActivitiesList3", partyActivitiesList3);
        model.addAttribute("vllageActivities1", vllageActivities1);
        model.addAttribute("vllageActivities2", vllageActivities2);
        model.addAttribute("vllageActivities3", vllageActivities3);
        model.addAttribute("villageCommitteeList", villageCommitteeList);
        model.addAttribute("villageCommitteeList2", villageCommitteeList2);
        model.addAttribute("zhenzhang", zhenzhang);
        model.addAttribute("shuji", shuji);
        model.addAttribute("waterscapeList", waterscapeList);
        model.addAttribute("buildingList", buildingList);
        model.addAttribute("foodList", foodList);
        return "index";
    }

}
